package cn.lls.service.impl;

import cn.lls.base.exception.LlsException;
import cn.lls.base.result.ResultCode;
import cn.lls.entity.company.DepartmentEntity;
import cn.lls.dao.DepartmentDao;
import cn.lls.entity.sys.EmployeeBaseEntity;
import cn.lls.service.IDepartmentService;
import cn.lls.service.IEmployeeBaseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 划水艺术家
 * @since 2022-11-15
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentDao, DepartmentEntity> implements IDepartmentService {

    @Autowired
    private IEmployeeBaseService employeeBaseService;

    @Override
    public void delDepartment(Long departmentId) {
        LambdaQueryWrapper<EmployeeBaseEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EmployeeBaseEntity::getDepartmentId, departmentId);
        List<EmployeeBaseEntity> list = employeeBaseService.list(queryWrapper);
        if (list != null && list.size() > 0)
            throw new LlsException(ResultCode.EXE_ERROR, "当前部门含员工，无法删除");
        try{
            int delete = this.baseMapper.deleteById(departmentId);
            if (delete == 0)
                throw new LlsException(ResultCode.DATA_ERROR, "数据异常");
        } catch (Exception e){
            throw new LlsException(ResultCode.DATA_ERROR, "数据异常");
        }
    }
}
